package com.utils.parsetxt.file;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.commons.codec.digest.DigestUtils;

public class FileUtil {

	//获得file的MD5
	public static String getFileMd5ByIn(InputStream in) {
		try {
			return DigestUtils.md5Hex(in);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	//将inputstream转file
	//https://blog.csdn.net/cyan20115/article/details/106554092
	public static File inToFile(InputStream in, String absoultDir, String fileName) {
		Path dir = Paths.get(absoultDir);
		if (!Files.exists(dir)) {
			try {
				Files.createDirectories(dir);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			Path filePath = Paths.get(absoultDir, fileName);
			boolean isExist = Files.exists(filePath);
			if (!isExist) {
//				System.out.println("file:"+filePath +" isexist:"+isExist);
				Files.copy(in, filePath);
			}
			return filePath.toFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
